#include "CompoundPropertiesDialog.h"

#include <QDebug>
#include <QFormLayout>
#include <QVariant>
#include <QMessageBox>

#include "Compound.h"

CompoundPropertiesDialog::CompoundPropertiesDialog(QListWidgetItem * item, QWidget * parent)
    : QDialog(parent), m_item(item)
{
    setModal(true);

    const Compound& cm = item->data(Qt::UserRole).value<Compound>();

    name_LE = new QLineEdit(cm.name);
    Eg_LE = new QLineEdit(QString::number(cm.Eg));
    a_lc_LE = new QLineEdit(QString::number(cm.a_lc));
    delta_so_LE = new QLineEdit(QString::number(cm.delta_so));
    vbo_LE = new QLineEdit(QString::number(cm.vbo));
    mass_ef_LE = new QLineEdit(QString::number(cm.mass_ef));
    ac_LE = new QLineEdit(QString::number(cm.ac));
    av_LE = new QLineEdit(QString::number(cm.av));
    b_LE = new QLineEdit(QString::number(cm.b));
    c11_LE = new QLineEdit(QString::number(cm.c11));
    c12_LE = new QLineEdit(QString::number(cm.c12));

    QFormLayout* form_layout = new QFormLayout();

    form_layout->addRow("Nazwa", name_LE);
    form_layout->addRow("Eg", Eg_LE);
    form_layout->addRow("a_lc", a_lc_LE);
    form_layout->addRow("Delta_SO", delta_so_LE);
    form_layout->addRow("VOB", vbo_LE);
    form_layout->addRow("m*", mass_ef_LE);
    form_layout->addRow("a_c", ac_LE);
    form_layout->addRow("a_v" ,av_LE);
    form_layout->addRow("b", b_LE);
    form_layout->addRow("c11", c11_LE);
    form_layout->addRow("c12", c12_LE);

    execute_button = new QPushButton("Ok!");
    cancel_button = new QPushButton("Anuluj");

    QVBoxLayout * main_layout = new QVBoxLayout();
    main_layout->addLayout(form_layout);
    QHBoxLayout * h_layout = new QHBoxLayout();
    h_layout->addWidget(execute_button);
    h_layout->addWidget(cancel_button);
    main_layout->addLayout(h_layout);

    this->setLayout(main_layout);

    connect(execute_button, SIGNAL(clicked()), this, SLOT(populate()));
    connect(cancel_button, SIGNAL(clicked()), this, SLOT(reject()));
}

void CompoundPropertiesDialog::populate(){

    bool succes_Eg, succes_a_lc, succes_delta_so, succes_vbo, succes_mass_ef, succes_ac, succes_av, succes_b, succes_c11, succes_c12;

    Compound temp_comp;
    temp_comp.name = name_LE->text();
    temp_comp.Eg = Eg_LE->text().toDouble(&succes_Eg);
    temp_comp.a_lc = a_lc_LE->text().toDouble(&succes_a_lc);
    temp_comp.delta_so = delta_so_LE->text().toDouble(&succes_delta_so);
    temp_comp.vbo = vbo_LE->text().toDouble(&succes_vbo);
    temp_comp.mass_ef = mass_ef_LE->text().toDouble(&succes_mass_ef);
    temp_comp.ac = ac_LE->text().toDouble(&succes_ac);
    temp_comp.av = av_LE->text().toDouble(&succes_av);
    temp_comp.b = b_LE->text().toDouble(&succes_b);
    temp_comp.c11 = c11_LE->text().toDouble(&succes_c11);
    temp_comp.c12 = c12_LE->text().toDouble(&succes_c12);

    if ( succes_ac && succes_av && succes_Eg && succes_a_lc && succes_b && succes_c11 && succes_c12 && succes_delta_so && succes_mass_ef && succes_vbo ) {
        QVariant v;
        v.setValue(temp_comp);

        m_item->setData(Qt::UserRole, v);
        m_item->setData(Qt::DisplayRole, name_LE->text());

        this->accept();
    }else{
        QString str;
        str = "Niepoprawne dane dla: \n";
        if(!succes_Eg)
            str += "Eg \n";
        if(!succes_a_lc)
            str += "a_lc \n";
        if(!succes_delta_so)
            str += "Delta_SO \n";
        if(!succes_vbo)
            str += "VBO \n";
        if(!succes_mass_ef)
            str += "m* \n";
        if(!succes_ac)
            str += "a_c \n";
        if(!succes_av)
            str += "a_v \n";
        if(!succes_b)
            str += "b \n";
        if(!succes_c11)
            str += "c11 \n";
        if(!succes_c12)
            str += "c12 \n";
        QMessageBox::critical(this,"Blad",str);
    }
}

CompoundPropertiesDialog::~CompoundPropertiesDialog(){}
